/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import delaunay_triangulation.Point_dt;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import model.estrutura.Ponto;
import model.estrutura.MalhaIrregular;

/**
 *
 * @author Thiago
 */
public class Draw extends JPanel implements Observer {

    Point_dt p1;
    Point_dt p2;
    Point_dt p3;

    String tipoVisao = "";

    double[][] m;
    Ponto[] p;
    int FPS;
    private Observable Picasso;
    MalhaIrregular estrutura = null;

    @Override
    public void paintComponent(Graphics g) {
        this.setDoubleBuffered(true);
        super.paintComponent(g);

        if (tipoVisao.equals("Perspectiva")) {
            g.drawString("Perspectiva", 20, 20);
        } else if (tipoVisao.equals("Isométrica")) {
            g.drawString("Isométrica", 20, 20);
        }
        if (m != null) {
            if (estrutura != null) {
                if (estrutura.getOcultacao()) {
                    int[] vx = new int[3];
                    int[] vy = new int[3];

                    int[] visivel = estrutura.getListaCor();

                    int index = 0;
                    for (int i = 0; i < this.m[0].length - 3; i += 3) {

                        vx[0] = (int) m[0][i];
                        vx[1] = (int) m[0][i + 1];
                        vx[2] = (int) m[0][i + 2];
                        vy[0] = (int) m[1][i];
                        vy[1] = (int) m[1][i + 1];
                        vy[2] = (int) m[1][i + 2];

                        if (visivel[index] != -1) {
                            g.setColor(new Color(0, 0, 0));
                            g.drawPolygon(vx, vy, 3);
                        }
                        index++;
                    }
                    
                } else {
                    int[] vx = new int[3];
                    int[] vy = new int[3];

                    int[] cores = estrutura.getListaCor();

                    int index = 0;
                    for (int i = 0; i < this.m[0].length - 3; i += 3) {

                        vx[0] = (int) m[0][i];
                        vx[1] = (int) m[0][i + 1];
                        vx[2] = (int) m[0][i + 2];
                        vy[0] = (int) m[1][i];
                        vy[1] = (int) m[1][i + 1];
                        vy[2] = (int) m[1][i + 2];

                        if (cores[index] != -1) {
                            int color = cores[index];
                            if (color > 255) {
                                color = 255;
                            }
                            if (color < 0) {
                                color = 0;
                            }
                            g.setColor(new Color(color, color, color));
                            g.fillPolygon(vx, vy, 3);
                        }
                        index++;
                    }
                }
            } else {
                for (int i = 0; i < this.m[0].length - 3; i += 3) {
                    g.drawLine((int) m[0][i], (int) m[1][i], (int) m[0][i + 1], (int) m[1][i + 1]);
                    g.drawLine((int) m[0][i], (int) m[1][i], (int) m[0][i + 2], (int) m[1][i + 2]);
                    g.drawLine((int) m[0][i + 2], (int) m[1][i + 2], (int) m[0][i + 1], (int) m[1][i + 1]);
                }
            }
        }
        if (p != null) {
            for (int i = 0; i < p.length; i++) {
                g.drawLine((int) (p[i].getX() + 500), (int) (p[i].getY() + 500), (int) (p[i].getX() + 500), (int) (p[i].getY() + 500));
            }
        }
        g.drawString("" + this.FPS + " FPS", 20, this.getHeight() - 20);
    }

    public void setMalha(double[][] m) {
        this.m = m;
    }

    public void setPontos(Ponto[] p) {
        this.p = p;
    }

    public void setTipoVisao(String visao) {
        this.tipoVisao = visao;
    }

    public void setPicasso(Picasso pic) {
        this.Picasso = pic;
        this.Picasso.addObserver(this);
    }

    public void setEstrutura(MalhaIrregular estrutura) {
        this.estrutura = estrutura;
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Picasso) {
            Picasso pic = (Picasso) o;
            this.FPS = pic.getFPS();
        }
    }
}
